package com.bw.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.finance.conf.ResponseResult;
import com.bw.finance.controller.EbankInvestorController;
import com.bw.finance.eneity.locana.EbankAccountLog;
import com.bw.finance.mapper.EbankAccountLogMapper;
import com.bw.finance.service.IEbankAccountLogService;
import com.bw.finance.utils.IdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxx
 * @since 2020-08-20
 */
@Service
public class EbankAccountLogServiceImpl extends ServiceImpl<EbankAccountLogMapper, EbankAccountLog> implements IEbankAccountLogService {

    private static final Logger log= LoggerFactory.getLogger(EbankInvestorController.class);
    @Resource
    private IEbankAccountLogService iEbankAccountLogService;
    @Autowired
    private IEbankAccountLogService ebankAccountLogService;

    @Override
    public ResponseResult getAccountLogList(int pagenum, int pagesize, EbankAccountLog ebankAccountLog) {
        ResponseResult result = ResponseResult.SUCCESS();
        Page<Object> page = PageHelper.startPage(pagenum, pagesize);
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            if (ebankAccountLog.getTradeNo()!=null){
                queryWrapper.like("TRADE_NO",ebankAccountLog.getTradeNo());
            }
            queryWrapper.eq("TYPE",0);
            List<EbankAccountLog> list = ebankAccountLogService.list(queryWrapper);
            PageInfo pageInfo = new PageInfo(list);
            result.setMessage("查询成功!");
            result.setResult(pageInfo);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询异常");
            e.printStackTrace();
            return result;
        }
        return result;
    }

    @Override
    public ResponseResult getAccountLogTixianList(int pagenum, int pagesize, EbankAccountLog ebankAccountLog) {
        ResponseResult result = ResponseResult.SUCCESS();
        Page<Object> page = PageHelper.startPage(pagenum, pagesize);
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            if (ebankAccountLog.getTradeNo()!=null){
                queryWrapper.like("TRADE_NO",ebankAccountLog.getTradeNo());
            }
            queryWrapper.eq("TYPE",1);
            List<EbankAccountLog> list = ebankAccountLogService.list(queryWrapper);
            PageInfo pageInfo = new PageInfo(list);
            result.setMessage("查询成功!");
            result.setResult(pageInfo);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询异常");
            e.printStackTrace();
            return result;
        }
        return result;
    }

    @Override
    public ResponseResult totixian(EbankAccountLog ebankAccountLog) {
        log.info("开始提现");
        ResponseResult result = ResponseResult.FAIL();
        try {
            IdWorker idWorker = new IdWorker(1, 1, 1);
            String code= String.valueOf(idWorker.nextId());
            EbankAccountLog ebankAccountLog1 = new EbankAccountLog();
            ebankAccountLog1.setAmount(ebankAccountLog.getAmount());//提现金额
            ebankAccountLog1.setUserId(ebankAccountLog.getUserId());//用户id
            ebankAccountLog1.setUserType(ebankAccountLog.getUserType());//用户类型
            ebankAccountLog1.setActionTime(new Date());//操作时间
            ebankAccountLog1.setType(1);//提现
            ebankAccountLog1.setDingdan_code(code);//提现订单编号
            ebankAccountLog1.setTxfy(ebankAccountLog1.getTxfy());//提现费用
            iEbankAccountLogService.saveOrUpdate(ebankAccountLog1);//添加提现历史记录

            result.setSuccess(true);
            result.setMessage("提现成功");
            log.info("提现结束");
        }catch (Exception e){
            result.setMessage("提现异常"+e.toString());
            log.error("提现异常"+e.toString());
            result.setSuccess(false);
            result.setMessage(e.toString());
        }
        return result;
    }

    @Override
    public ResponseResult findallmoney(int id) {
        log.info("开始充值");
        ResponseResult result = ResponseResult.FAIL();
        try {
            //////////////////////////////////////计算充值金额
            QueryWrapper<EbankAccountLog> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(EbankAccountLog::getUserId, id)
                    .eq(EbankAccountLog::getType, 0);
            List<EbankAccountLog> list = iEbankAccountLogService.list(queryWrapper);
            BigDecimal count = new BigDecimal(0);//总金额
            BigDecimal amount = new BigDecimal(0);//单个历史记录金额
            for (EbankAccountLog list1 : list) {
                amount = list1.getAmount();
                count = count.add(amount);
            }
            /////////////////////////////////////// 计算提现金额

            QueryWrapper<EbankAccountLog> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.lambda().eq(EbankAccountLog::getUserId, id).eq(EbankAccountLog::getType, 2);
            List<EbankAccountLog> list2 = iEbankAccountLogService.list(queryWrapper2);
            BigDecimal count2 = new BigDecimal(0);//总金额
            BigDecimal amount2 = new BigDecimal(0);//单个历史记录金额
            for (EbankAccountLog list02 : list2) {
                amount2 = list02.getAmount();
                count2 = count2.add(amount2);
            }
            BigDecimal count3 = count.subtract(count2);
            result.setSuccess(true);
            result.setResult(count3);
            log.info("充值结束");
        } catch (Exception e) {
            log.error("充值异常" + e.toString());
            result.setSuccess(false);
            result.setMessage(e.toString());
        }
        return result;
    }};